package com.whfp.coky.manager

import android.app.Activity
import android.os.Bundle
import com.hjq.demo.action.ToastAction
import com.tencent.mmkv.MMKV
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.main.TaskMainAct
import com.whfp.coky.ui.task.model.TaskChildBean

/**
 *
 * 任务执行管理类
 * created by coky on 2022/3/15
 */
class TaskManager private constructor(val taskBeans: List<TaskChildBean>): ToastAction {

    val mmkv: MMKV = MMKV.mmkvWithID(Constants.MMKV_TASK)
    var currTaskIndex: Int = 0
    private var taskSize: Int
        get() = field

    init {
        taskSize = taskBeans.size
    }


    companion object {
        private var instance: TaskManager? = null
        fun newInstance(tb: List<TaskChildBean>): TaskManager {
            if (instance == null) {
                instance = TaskManager(tb)
            }
            return instance!!
        }
    }


    /**
     * 任务启动
     */
    fun taskStart(context: BaseActivity<*,*>,finishCurrAct:Boolean = true) {
        val taskItem = taskBeans.get(currTaskIndex)
        targetAct(taskItem)?.let {
                context.startActivity(it,Bundle().apply {
                    putSerializable(Constants.BEAN, taskItem)
                    putString(Constants.TITLE, targetTitle(taskItem))
                })
                if (finishCurrAct){
                    context.finish()
                }
        }
    }

    /**
     * 执行下一个任务
     *
     */
    fun toNext(context: BaseActivity<*,*>,finishCurrAct:Boolean) {
        currTaskIndex += 1
        if (currTaskIndex >= taskSize){
            allTaskFinished()
        }else{
            taskStart(context,finishCurrAct)
        }
    }

    /**
     * 任务全部完成
     */
    private fun allTaskFinished() {
        over()
    }

    /**
     * 任务完成，释放对象
     */
    fun over() {
        instance = null
    }








    /**
     * 获取目标act
     */
    private fun targetAct(taskItem: TaskChildBean): Class<out Activity>? {
        val clazz = when (taskItem.taskDetailType) {
            Constants.TASK_NUMBER_BQZD -> TaskMainAct::class.java//标曲制定
            Constants.TASK_NUMBER_SM -> TaskMainAct::class.java//扫码
            Constants.TASK_NUMBER_HXPXX -> TaskMainAct::class.java//化学品信息
            Constants.TASK_NUMBER_KF -> TaskMainAct::class.java//库房
            Constants.TASK_NUMBER_CLXX -> TaskMainAct::class.java//车辆信息
            Constants.TASK_NUMBER_WZJC -> TaskMainAct::class.java//物质检查
            Constants.TASK_NUMBER_RYXXTX -> TaskMainAct::class.java//人员信息填写
            Constants.TASK_NUMBER_RFID_BQXK -> TaskMainAct::class.java//RFID标签寻卡
            Constants.TASK_NUMBER_RFID_BQDK -> TaskMainAct::class.java//RFID标签读卡
            Constants.TASK_NUMBER_RFID_BQ_XIEKA -> TaskMainAct::class.java//RFID标签写卡
            Constants.TASK_NUMBER_RFID_BQSD -> TaskMainAct::class.java//RFID标签锁定
            Constants.TASK_NUMBER_RFID_BQJS -> TaskMainAct::class.java//RFID标签解锁
            Constants.TASK_NUMBER_YGJC -> TaskMainAct::class.java//荧光检测
            Constants.TASK_NUMBER_SFZPZSBXX -> TaskMainAct::class.java//身份证采集
            Constants.TASK_NUMBER_RLCJ -> TaskMainAct::class.java//人脸采集
            Constants.TASK_NUMBER_DZQM -> TaskMainAct::class.java//签名采集
            Constants.TASK_NUMBER_RLHY -> TaskMainAct::class.java//人脸核验 
            Constants.TASK_NUMBER_MFCJBXX -> TaskMainAct::class.java//毛发采集表信息
            Constants.TASK_NUMBER_PZ -> TaskMainAct::class.java//拍照上传
            Constants.TASK_NUMBER_EWMBD -> TaskMainAct::class.java//二维码绑定
            Constants.TASK_NUMBER_EWMSC -> TaskMainAct::class.java//二维码生成
            Constants.TASK_NUMBER_EWMDY -> TaskMainAct::class.java//二维码打印 
            else -> null
        }

        if (clazz == null) {
            toast("当前设备未收录此子任务")
            over()
        }
        return clazz
    }


    private fun targetTitle(taskItem: TaskChildBean): String {
        return when (taskItem.taskDetailType) {
            Constants.TASK_NUMBER_BQZD -> Constants.INTENT_TYPE_TASK_BQZD//标曲制定
            Constants.TASK_NUMBER_SM ->  Constants.INTENT_TYPE_TASK_SM//扫码
            Constants.TASK_NUMBER_HXPXX ->  Constants.INTENT_TYPE_TASK_HXPXX//化学品信息
            Constants.TASK_NUMBER_KF ->  Constants.INTENT_TYPE_TASK_KF//库房
            Constants.TASK_NUMBER_CLXX ->  Constants.INTENT_TYPE_TASK_CLXX//车辆信息
            Constants.TASK_NUMBER_WZJC ->  Constants.INTENT_TYPE_TASK_WZJC//物质检查
            
            Constants.TASK_NUMBER_RYXXTX->  Constants.INTENT_TYPE_TASK_RYXXTX//人员信息填写
            Constants.TASK_NUMBER_RFID_BQXK->  Constants.INTENT_TYPE_TASK_RFID_BQXK//RFID标签寻卡
            Constants.TASK_NUMBER_RFID_BQDK->  Constants.INTENT_TYPE_TASK_RFID_BQDK//RFID标签读卡
            Constants.TASK_NUMBER_RFID_BQ_XIEKA->  Constants.INTENT_TYPE_TASK_RFID_BQXIEKA//RFID标签写卡
            Constants.TASK_NUMBER_RFID_BQSD->  Constants.INTENT_TYPE_TASK_RFID_BQSD//RFID标签锁定
            Constants.TASK_NUMBER_RFID_BQJS->  Constants.INTENT_TYPE_TASK_RFID_BQJS//RFID标签解锁
            Constants.TASK_NUMBER_YGJC->  Constants.INTENT_TYPE_TASK_YGJC//荧光检测
            Constants.TASK_NUMBER_SFZPZSBXX->  Constants.INTENT_TYPE_TASK_SFZCJ//身份证采集
            Constants.TASK_NUMBER_RLCJ->  Constants.INTENT_TYPE_TASK_RLCJ//人脸采集
            Constants.TASK_NUMBER_DZQM->  Constants.INTENT_TYPE_TASK_QMCJ//签名采集
            Constants.TASK_NUMBER_RLHY->  Constants.INTENT_TYPE_TASK_RLHY//人脸核验
            Constants.TASK_NUMBER_MFCJBXX->  Constants.INTENT_TYPE_TASK_RLHY//毛发采集表信息???????????????????????
            Constants.TASK_NUMBER_PZ->  Constants.INTENT_TYPE_TASK_PZSC//拍照上传
            Constants.TASK_NUMBER_EWMBD->  Constants.INTENT_TYPE_TASK_EWMBD//二维码绑定
            Constants.TASK_NUMBER_EWMSC->  Constants.INTENT_TYPE_TASK_EWMSC//二维码生成
            Constants.TASK_NUMBER_EWMDY->  Constants.INTENT_TYPE_TASK_DYEWM//二维码打印
            else -> "臻稀物联"
        }
    }
}
